package com.ruoyi.web.controller.system.biz;

import java.util.Date;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.fastjson2.JSON;
import com.ruoyi.common.core.domain.entity.SysRole;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.spring.SpringUtils;
import com.ruoyi.system.domain.dto.AssessDTO;
import com.ruoyi.system.domain.dto.CommitAssessDTO;
import com.ruoyi.system.service.IAssessService;
import com.ruoyi.system.service.ISysRoleService;
import org.springframework.beans.BeanUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.Assess;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 【请填写功能名称】Controller
 *
 * @author ruoyi
 * @date 2025-04-22
 */
@RestController
@RequestMapping("/system/assess")
public class AssessController extends BaseController {
    @Autowired
    private IAssessService assessService;
    @Autowired
    private ISysRoleService sysRoleService;

    /**
     * 查询【请填写功能名称】列表
     */
    @GetMapping("/list")
    public TableDataInfo list(Assess assess) {
        startPage();
        LoginUser loginUser = getLoginUser();
        assess.setTaskCreator(loginUser.getUserId().toString());
        assess.setCreateDept(getDeptId() == null ? null : getDeptId().toString());
        List<Assess> list = assessService.selectAssessList(assess);
        return getDataTable(list);
    }

    /**
     * 导出【请填写功能名称】列表
     */
    @PreAuthorize("@ss.hasPermi('system:assess:export')")
    @Log(title = "【请填写功能名称】", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Assess assess) {
        List<Assess> list = assessService.selectAssessList(assess);
        ExcelUtil<Assess> util = new ExcelUtil<Assess>(Assess.class);
        util.exportExcel(response, list, "【请填写功能名称】数据");
    }

    /**
     * 获取【请填写功能名称】详细信息
     */
    //@PreAuthorize("@ss.hasPermi('system:assess:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(assessService.selectAssessById(id));
    }

    /**
     * 新增【请填写功能名称】
     */
    @Log(title = "【请填写功能名称】", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody AssessDTO assessDTO) {
        LoginUser loginUser = getLoginUser();
        List<Long> roleIds = sysRoleService.selectRoleListByUserId(loginUser.getUserId());
        if (roleIds.isEmpty()) {
            throw new ServiceException("当前账号无角色信息");
        }
        Assess assess = new Assess();
        BeanUtils.copyProperties(assessDTO, assess);
        assess.setScaleIds(JSON.toJSONString(assessDTO.getScaleIdList()));
        assess.setOpenTarget(JSON.toJSONString(assessDTO.getOpenTargetList()));
        assess.setTaskCreator(loginUser.getUserId().toString());
        return toAjax(assessService.insertAssess(assess));
    }

    /**
     * 修改【请填写功能名称】
     */
    @Log(title = "【请填写功能名称】", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody AssessDTO assessDTO) {
        String result = assessService.editAssess(assessDTO);
        return AjaxResult.success(result);
    }


    /**
     * 修改【请填写功能名称】
     */
    @Log(title = "【请填写功能名称】", businessType = BusinessType.UPDATE)
    @PostMapping("/updateAssess")
    public AjaxResult edit(@RequestBody Assess assess) {
        Assess assess1 = new Assess();
        assess1.setId(assess.getId());
        assess1.setIsOpen(assess.getIsOpen());
        return toAjax(assessService.updateAssess(assess1));
    }

    /**
     * 删除【请填写功能名称】
     */
   // @PreAuthorize("@ss.hasPermi('system:assess:remove')")
    @Log(title = "【请填写功能名称】", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(assessService.deleteAssessByIds(ids));
    }


    /**
     * 带我处理的任务
     */
    @GetMapping("/pending/list")
    public TableDataInfo pendingList(Assess assess) {
        startPage();
        LoginUser loginUser = getLoginUser();
        assess.setDepartmentName(loginUser.getUserId().toString());
        assess.setIsOpen("1");
        assess.setStatus(0);
        List<Assess> list = assessService.selectAssessList(assess);
        return getDataTable(list);
    }

    /**
     * 带我处理的任务
     */
    @GetMapping("/complete/list")
    public TableDataInfo completeList(Assess assess) {
        startPage();
        LoginUser loginUser = getLoginUser();
        assess.setDepartmentName(loginUser.getUserId().toString());
        assess.setIsOpen("1");
        assess.setStatus(1);
        List<Assess> list = assessService.selectAssessList(assess);
        return getDataTable(list);
    }

    /**
     * 办理任务提交
     */
    @PostMapping("/taskCommit")
    public AjaxResult commitAssess(@RequestBody CommitAssessDTO assessDTO) {
        return toAjax(assessService.commitAssess(assessDTO));
    }


    /**
     * 导出任务，用户完成情况
     */
    @PostMapping("/task/overview/export")
    public void exportTaskOverview(HttpServletResponse response, Long taskId) {
        if(taskId == null){
            throw new ServiceException("请选择任务");
        }
        assessService.exportTaskOverview(response,taskId);
    }

}
